package gmusic.mb.services.impl;

import gmusic.mb.bo.ArtistMBBean;
import gmusic.mb.bo.ReleaseMBBean;
import gmusic.mb.bo.TrackMBBean;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.dom4j.DocumentException;

public class MusicBrainzWebServices {

	public static final String URL_GET_ARTIST = "http://musicbrainz.org/ws/1/artist/PARAM?type=xml";

	public static final String URL_GET_ARTISTS = "http://musicbrainz.org/ws/1/artist/?query=PARAM&type=xml";

	public static final String URL_GET_RELEASE = "http://musicbrainz.org/ws/1/release/PARAM?type=xml";

	public static final String URL_GET_RELEASES = "http://musicbrainz.org/ws/1/release/?artistid=PARAM&type=xml";

	public static final String URL_GET_TRACK = "http://musicbrainz.org/ws/1/track/PARAM?type=xml";

	public static final String URL_GET_TRACKS = "http://musicbrainz.org/ws/1/track/?releaseid=PARAM&type=xml";

	private static MusicBrainzWebServices instance = null;

	private MusicBrainzWebServices() {

	}

	public static MusicBrainzWebServices getInstance() {
		if (instance == null) {
			instance = new MusicBrainzWebServices();
		}
		return instance;
	}

	public List<ArtistMBBean> getArtists(String nom) throws IOException,
			DocumentException {

		nom = nom.replaceAll(" ", "+");
		URL fileURL = new URL(URL_GET_ARTISTS.replaceFirst("PARAM", nom));
		MusicBrainzXMLParser parser = new MusicBrainzXMLParser(fileURL);
		return parser.getArtists();
	}

	public ArtistMBBean getArtist(String ambid) throws IOException,
			DocumentException {

		URL fileURL = new URL("http://musicbrainz.org/ws/1/artist/" + ambid
				+ "?type=xml");
		MusicBrainzXMLParser parser = new MusicBrainzXMLParser(fileURL);
		return parser.getArtist();
	}

	public List<ReleaseMBBean> getReleases(String ambid) throws IOException,
			DocumentException {

		URL fileURL = new URL(URL_GET_RELEASES.replaceFirst("PARAM", ambid));
		MusicBrainzXMLParser parser = new MusicBrainzXMLParser(fileURL);
		return parser.getReleases();
	}

	public ReleaseMBBean getRelease(String rmbid) throws IOException,
			DocumentException {

		URL fileURL = new URL(URL_GET_RELEASE.replaceFirst("PARAM", rmbid));
		MusicBrainzXMLParser parser = new MusicBrainzXMLParser(fileURL);
		return parser.getRelease();
	}

	public List<TrackMBBean> getTracks(String rmbid) throws IOException,
			DocumentException {

		URL fileURL = new URL(URL_GET_TRACKS.replaceFirst("PARAM", rmbid));
		MusicBrainzXMLParser parser = new MusicBrainzXMLParser(fileURL);
		List<TrackMBBean> tracks = parser.getTracks();
		for (int i = 0; i < tracks.size(); i++) {
			tracks.get(i).setReleaseMbid(rmbid);
		}
		return tracks;
	}

	public TrackMBBean getTrack(String tmbid) throws IOException,
			DocumentException {

		URL fileURL = new URL(URL_GET_TRACK.replaceFirst("PARAM", tmbid));
		MusicBrainzXMLParser parser = new MusicBrainzXMLParser(fileURL);
		return parser.getTrack();
	}

	public static void main(String[] args) throws IOException,
			DocumentException {
		// Tests unitaires
		System.out.println("### Recherche de 'tori amos' :");
		List<ArtistMBBean> artists = getInstance().getArtists("tori amos");
		for (int i = 0; i < artists.size(); i++) {
			System.out.println("   " + artists.get(i));
		}

		System.out.println("### Recherche d'un groupe :");
		ArtistMBBean artist = getInstance().getArtist(
				"49cd96a6-42c3-44f6-ba2a-cd9301046b96");
		System.out.println("   " + artist);

		System.out.println("### Recherche des releases d'un artiste :");
		List<ReleaseMBBean> releases = getInstance().getReleases(
				"49cd96a6-42c3-44f6-ba2a-cd9301046b96");
		for (int i = 0; i < releases.size(); i++) {
			System.out.println("   " + releases.get(i));
		}

		System.out.println("### Recherche d'une release :");
		ReleaseMBBean release = getInstance().getRelease(
				"15ee494f-fd13-49bf-8667-23de8c8eaf65");
		System.out.println("   " + release);

		System.out.println("### Recherche des titres d'une release :");
		List<TrackMBBean> tracks = getInstance().getTracks(
				"15ee494f-fd13-49bf-8667-23de8c8eaf65");
		for (int i = 0; i < tracks.size(); i++) {
			System.out.println("   " + tracks.get(i));
		}

		System.out.println("### Recherche d'une release :");
		TrackMBBean track = getInstance().getTrack(
				"297d5100-9fd1-4d4d-9c4a-ba0b90502649");
		System.out.println("   " + track);

	}
}
